<script setup lang="ts">
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import type { KnowledgeType, KnowledgeList } from '@/types/consult'
import { getKnowledgeList } from '@/services/consult'

const props = defineProps<{
  type: KnowledgeType
}>()

const loading = ref(false) // 控制加载中效果 (必须双向绑定给组件 组件内部需要更新数据)
const finished = ref(false) // 控制数据是否加载完成

const list = ref<KnowledgeList>([])

const params = ref({
  type: props.type,
  current: 1,
  pageSize: 10
})

// 一旦组件触底 自动触发load事件 将loading改成true
const loadList = async () => {
  // 请求更多数据 => 数据回来了 => 一定追加数据 => 页码++ => 将loading改false => 额外处理数据全部加载完成的状态 改finished
  const res = await getKnowledgeList(params.value)

  // 页码++
  params.value.current++

  // console.log(res.data.rows)
  list.value.push(...res.data.rows)
  loading.value = false

  if (list.value.length === res.data.total) {
    finished.value = true // 加载彻底结束了
  }
}
</script>

<template>
  <div class="knowledge-list">
    <!-- van-list 包裹长列表就会有上啦加载的效果 -->
    <van-list
      :finished="finished"
      v-model:loading="loading"
      @load="loadList"
      finished-text="我是有底线的"
    >
      <knowledge-card
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
